{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.],\n",
       "       [0.],\n",
       "       [0.]])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.zeros((3, 1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[Taichi] Starting on arch=x64\n",
      "getA: 1000.000000\n",
      "getA: 121.000000\n"
     ]
    }
   ],
   "source": [
    "#This verified taichi class can call other class's function\n",
    "import taichi as ti\n",
    "ti.init(arch=ti.cpu)\n",
    "@ti.data_oriented\n",
    "class AClass:\n",
    "    def __init__(self):\n",
    "        self.a = ti.field(ti.f32, shape=())\n",
    "        self.a[None] = 1000\n",
    "    @ti.func\n",
    "    def getA(self):\n",
    "        return self.a[None]\n",
    "    \n",
    "    def setA(self, a):\n",
    "        self.a[None] = a\n",
    "\n",
    "@ti.data_oriented\n",
    "class BClass:\n",
    "    def __init__(self, aobj: AClass):\n",
    "        self.aobj = aobj\n",
    "    @ti.kernel\n",
    "    def work(self):\n",
    "        print(\"getA:\", self.aobj.getA())\n",
    "    \n",
    "a = AClass()\n",
    "b = BClass(a)\n",
    "b.work()\n",
    "a.setA(121)\n",
    "b.work()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[Taichi] Starting on arch=x64\n",
      "10\n"
     ]
    }
   ],
   "source": [
    "import taichi as ti\n",
    "import numpy as np\n",
    "@ti.kernel\n",
    "def test(n: ti.types.ndarray()):\n",
    "    print(n.shape[0])\n",
    "\n",
    "ti.init()\n",
    "test(np.zeros((10, 10)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[Taichi] Starting on arch=x64\n",
      "0.000000\n"
     ]
    }
   ],
   "source": [
    "import taichi as ti\n",
    "ti.init(arch=ti.cpu)\n",
    "a = ti.field(ti.f32, shape=())\n",
    "@ti.kernel\n",
    "def test(n: ti.template()):\n",
    "    print(n[None])\n",
    "test(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[Taichi] Starting on arch=x64\n",
      "[0.000000, 0.000000, 0.000000]\n",
      "[1.000000, 1.000000, 1.000000]\n",
      "[2.000000, 2.000000, 2.000000]\n",
      "[3.000000, 3.000000, 3.000000]\n",
      "[4.000000, 4.000000, 4.000000]\n",
      "[5.000000, 5.000000, 5.000000]\n",
      "[6.000000, 6.000000, 6.000000]\n",
      "[7.000000, 7.000000, 7.000000]\n",
      "[8.000000, 8.000000, 8.000000]\n",
      "[9.000000, 9.000000, 9.000000]\n"
     ]
    }
   ],
   "source": [
    "#This verified taichi class can call other class's function\n",
    "import taichi as ti\n",
    "ti.init(arch=ti.cpu)\n",
    "vec3 = ti.types.vector(3, float)\n",
    "\n",
    "@ti.dataclass\n",
    "class Sphere:\n",
    "    center: vec3\n",
    "    radius: ti.f32\n",
    "\n",
    "    @ti.func\n",
    "    def area(self):\n",
    "        # a function to run in taichi scope\n",
    "        return 4 * math.pi * self.radius * self.radius\n",
    "\n",
    "    def is_zero_sized(self):\n",
    "        # a python scope function\n",
    "        return self.radius == 0.0\n",
    "    @ti.func\n",
    "    def set_center(self, center):\n",
    "        self.center = center\n",
    "n = 10\n",
    "sphere_field = Sphere.field(shape=(n,))\n",
    "@ti.kernel\n",
    "def test():\n",
    "    for i in range(n):\n",
    "        sphere_field[i].set_center(vec3(i, i, i))\n",
    "        s = sphere_field[i]\n",
    "        s.set_center(vec3(i + 3, i, i))\n",
    "    for i in range(n):\n",
    "        print(sphere_field[i].center)\n",
    "test()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "neigh [[11 34  5]\n",
      " [ 9  2 22]\n",
      " [10  1 25]\n",
      " [18  4 29]\n",
      " [12  3  5]\n",
      " [ 0 30  4]\n",
      " [13 20  7]\n",
      " [31  8  6]\n",
      " [24  7 21]\n",
      " [ 1 10 32]\n",
      " [ 2  9 11]\n",
      " [ 0 14 10]\n",
      " [ 4 13 16]\n",
      " [ 6 12 17]\n",
      " [11 35 15]\n",
      " [33 17 14]\n",
      " [12 36 17]\n",
      " [13 15 16]\n",
      " [ 3 20 19]\n",
      " [23 18 21]\n",
      " [ 6 18 21]\n",
      " [ 8 19 20]\n",
      " [ 1 24 27]\n",
      " [19 26 24]\n",
      " [ 8 22 23]\n",
      " [ 2 39 27]\n",
      " [23 38 27]\n",
      " [22 25 26]\n",
      " [37 29 30]\n",
      " [ 3 28 30]\n",
      " [ 5 28 29]\n",
      " [ 7 32 33]\n",
      " [ 9 31 33]\n",
      " [15 31 32]\n",
      " [ 0 35 36]\n",
      " [14 34 36]\n",
      " [16 34 35]\n",
      " [28 38 39]\n",
      " [26 37 39]\n",
      " [25 37 38]]\n"
     ]
    }
   ],
   "source": [
    "#Test quick hull\n",
    "%matplotlib qt\n",
    "from scipy.spatial import ConvexHull, convex_hull_plot_2d\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from mpl_toolkits.mplot3d import Axes3D, art3d\n",
    "\n",
    "rng = np.random.default_rng()\n",
    "points = rng.random((30, 3))   # 30 random points in 2-D\n",
    "hull = ConvexHull(points)\n",
    "faces = hull.simplices\n",
    "vertices = hull.points\n",
    "print(\"neigh\", hull.neighbors)\n",
    "\n",
    "fig = plt.figure()\n",
    "ax = fig.add_subplot(projection=\"3d\")\n",
    "\n",
    "pc = art3d.Poly3DCollection(vertices[faces], edgecolor=\"black\")\n",
    "ax.add_collection(pc)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0., 0., 0.],\n",
       "       [0., 0., 0.],\n",
       "       [0., 0., 0.],\n",
       "       [0., 0., 0.],\n",
       "       [0., 0., 0.],\n",
       "       [0., 0., 0.],\n",
       "       [0., 0., 0.],\n",
       "       [0., 0., 0.],\n",
       "       [0., 0., 0.],\n",
       "       [0., 0., 0.],\n",
       "       [0., 0., 0.],\n",
       "       [0., 0., 0.],\n",
       "       [0., 0., 0.],\n",
       "       [0., 0., 0.],\n",
       "       [0., 0., 0.],\n",
       "       [0., 0., 0.],\n",
       "       [0., 0., 0.],\n",
       "       [0., 0., 0.],\n",
       "       [0., 0., 0.],\n",
       "       [0., 0., 0.],\n",
       "       [0., 0., 0.],\n",
       "       [0., 0., 0.],\n",
       "       [0., 0., 0.],\n",
       "       [0., 0., 0.],\n",
       "       [0., 0., 0.],\n",
       "       [0., 0., 0.],\n",
       "       [0., 0., 0.],\n",
       "       [0., 0., 0.],\n",
       "       [0., 0., 0.],\n",
       "       [0., 0., 0.],\n",
       "       [0., 0., 0.],\n",
       "       [0., 0., 0.],\n",
       "       [0., 0., 0.],\n",
       "       [0., 0., 0.],\n",
       "       [0., 0., 0.],\n",
       "       [0., 0., 0.],\n",
       "       [0., 0., 0.],\n",
       "       [0., 0., 0.],\n",
       "       [0., 0., 0.],\n",
       "       [0., 0., 0.],\n",
       "       [0., 0., 0.],\n",
       "       [0., 0., 0.],\n",
       "       [0., 0., 0.],\n",
       "       [0., 0., 0.],\n",
       "       [0., 0., 0.],\n",
       "       [0., 0., 0.],\n",
       "       [0., 0., 0.],\n",
       "       [0., 0., 0.],\n",
       "       [0., 0., 0.],\n",
       "       [0., 0., 0.],\n",
       "       [0., 0., 0.],\n",
       "       [0., 0., 0.],\n",
       "       [0., 0., 0.],\n",
       "       [0., 0., 0.],\n",
       "       [0., 0., 0.],\n",
       "       [0., 0., 0.],\n",
       "       [0., 0., 0.],\n",
       "       [0., 0., 0.],\n",
       "       [0., 0., 0.],\n",
       "       [0., 0., 0.],\n",
       "       [0., 0., 0.],\n",
       "       [0., 0., 0.],\n",
       "       [0., 0., 0.],\n",
       "       [0., 0., 0.],\n",
       "       [0., 0., 0.],\n",
       "       [0., 0., 0.],\n",
       "       [0., 0., 0.],\n",
       "       [0., 0., 0.],\n",
       "       [0., 0., 0.],\n",
       "       [0., 0., 0.],\n",
       "       [0., 0., 0.],\n",
       "       [0., 0., 0.],\n",
       "       [0., 0., 0.],\n",
       "       [0., 0., 0.],\n",
       "       [0., 0., 0.],\n",
       "       [0., 0., 0.],\n",
       "       [0., 0., 0.],\n",
       "       [0., 0., 0.],\n",
       "       [0., 0., 0.],\n",
       "       [0., 0., 0.],\n",
       "       [0., 0., 0.],\n",
       "       [0., 0., 0.],\n",
       "       [0., 0., 0.],\n",
       "       [0., 0., 0.],\n",
       "       [0., 0., 0.],\n",
       "       [0., 0., 0.],\n",
       "       [0., 0., 0.],\n",
       "       [0., 0., 0.],\n",
       "       [0., 0., 0.],\n",
       "       [0., 0., 0.],\n",
       "       [0., 0., 0.],\n",
       "       [0., 0., 0.],\n",
       "       [0., 0., 0.],\n",
       "       [0., 0., 0.],\n",
       "       [0., 0., 0.],\n",
       "       [0., 0., 0.],\n",
       "       [0., 0., 0.],\n",
       "       [0., 0., 0.],\n",
       "       [0., 0., 0.],\n",
       "       [0., 0., 0.]])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "a = np.zeros((100, 3))\n",
    "b = np.zeros(100)\n",
    "a * b[:, None]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<mpl_toolkits.mplot3d.art3d.Path3DCollection at 0x7f17e8be4040>"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def generate_uniform_sample_points(sampling_density):\n",
    "    phi = np.pi * (3 - np.sqrt(5))\n",
    "    ret = []\n",
    "    for i in range(sampling_density):\n",
    "        y = 1 - 2 * (i / (sampling_density - 1))\n",
    "        radius = np.sqrt(1 - y * y)\n",
    "        theta = phi * i\n",
    "        x = np.cos(theta) * radius\n",
    "        z = np.sin(theta) * radius\n",
    "        ret.append([x, y, z])\n",
    "    return np.array(ret, dtype=np.float32)\n",
    "ret = generate_uniform_sample_points(64)\n",
    "fig = plt.figure()\n",
    "ax = fig.add_subplot(projection='3d')\n",
    "ax.scatter(ret[:,0], ret[:,1], ret[:,2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(64, 3)"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ret.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[Taichi] Starting on arch=x64\n",
      "[3, 2, 3, 2, -4]\n"
     ]
    }
   ],
   "source": [
    "import taichi as ti\n",
    "ti.init(arch=ti.cpu)\n",
    "@ti.kernel\n",
    "def test():\n",
    "    vec = ti.Vector([3.9, 2.3, 3.0, 2.9999, -3.2], ti.f32)\n",
    "    print(ti.floor(vec, ti.i32))\n",
    "test()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[Taichi] Starting on arch=x64\n",
      "0 0 0\n",
      "0 0 1\n",
      "0 1 0\n",
      "0 1 1\n",
      "1 0 0\n",
      "1 0 1\n",
      "1 1 0\n",
      "1 1 1\n"
     ]
    }
   ],
   "source": [
    "import taichi as ti\n",
    "ti.init(arch=ti.cpu)\n",
    "@ti.kernel\n",
    "def test():\n",
    "    for di in ti.static(range(2)):\n",
    "        for dj in ti.static(range(2)):\n",
    "            for dk in ti.static(range(2)):\n",
    "                print(di, dj, dk)\n",
    "test()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "'type' object is not subscriptable",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m/home/xuhao/d2slam_ws/src/TaichiSLAM/tests/test.ipynb Cell 13\u001b[0m in \u001b[0;36m<cell line: 1>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> <a href='vscode-notebook-cell:/home/xuhao/d2slam_ws/src/TaichiSLAM/tests/test.ipynb#X15sZmlsZQ%3D%3D?line=0'>1</a>\u001b[0m \u001b[39mlist\u001b[39;49m[\u001b[39mrange\u001b[39;49m(\u001b[39m1\u001b[39;49m)]\n",
      "\u001b[0;31mTypeError\u001b[0m: 'type' object is not subscriptable"
     ]
    }
   ],
   "source": [
    "list[range(1)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.8.5 ('base')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "08ce52785f0fedc81003ce387e097a83d6cc9494681cd746006386992005bb71"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
